package cn.lunkr.sdkdemo.ui.table.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import org.apache.commons.lang.StringUtils;

import java.util.Date;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentResultListener;
import androidx.fragment.app.FragmentTransaction;
import cn.lunkr.base.entity.mail.MailCalendar;
import cn.lunkr.base.newnet.result.CalListAccessedCalFolderResult;
import cn.lunkr.base.ui.BasicFragment;
import cn.lunkr.base.util.DateUtil;
import cn.lunkr.mailui.ui.calendar.ui.CalendarDetailFragment;
import cn.lunkr.mailui.ui.calendar.ui.ScheduleBarFragment;
import cn.lunkr.mailui.ui.calendar.ui.ScheduleFragment;
import cn.lunkr.sdkdemo.R;
import cn.lunkr.sdkdemo.databinding.FragmentTableScheduleBinding;


public class TableScheduleFragment extends BasicFragment implements FragmentResultListener,
        CalendarDetailFragment.DetailCallBack, ScheduleBarFragment.ScheduleBarFragmentListener {

    private FragmentTableScheduleBinding viewBinding;
    private String mYYMMDD;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        viewBinding = FragmentTableScheduleBinding.inflate(getLayoutInflater());
        initFragAction();
        return viewBinding.getRoot();
    }

    private void initFragAction() {
        requireActivity().getSupportFragmentManager().setFragmentResultListener(ScheduleFragment.INTO_CALENDAR_DETAIL, this, this);
    }

    @Override
    public void onFragmentResult(@NonNull String requestKey, @NonNull Bundle result) {
        FragmentTransaction ft = getChildFragmentManager().beginTransaction();
        if (StringUtils.equals(ScheduleFragment.INTO_CALENDAR_DETAIL, requestKey)) {
            CalendarDetailFragment fragment = new CalendarDetailFragment();
            fragment.setArguments(result);
            ft.replace(R.id.detail_container, fragment);
        }
        if (viewBinding.slidingPaneLayout.isOpen()) {
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        }
        ft.commit();
        viewBinding.slidingPaneLayout.open();
    }

    @Override
    public void showEmpty() {
        EmptyFragment emptyFragment = new EmptyFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(EmptyFragment.EMPTY_TYPE, 0);
        emptyFragment.setArguments(bundle);
        getChildFragmentManager().beginTransaction().replace(R.id.detail_container, emptyFragment).commit();
    }

    @Override
    public void finishDetailPage() {
        removeDetailContainer();
        // 删除日程，需要刷新列表
        Fragment mailFrag = getChildFragmentManager().findFragmentById(R.id.list_container);
        if (mailFrag instanceof ScheduleBarFragment) {
            ((ScheduleBarFragment) mailFrag).resetScheduleList();
        }
    }

    @Override
    public void setCurrentSelectDate(Date date) {
        String YYMMDD = DateUtil.getYYYYMDDate(date.getTime());
        if(!YYMMDD.equals(mYYMMDD)) {
            mYYMMDD = YYMMDD;
            removeDetailContainer();
        }
    }

    @Override
    public void onItemClick(int index, CalListAccessedCalFolderResult.ListAccessedCalFolderModel model, MailCalendar mailCalendar) {
        Bundle bundle = new Bundle();
        bundle.putParcelable("mail_calendar", mailCalendar);
        bundle.putInt("index", index);
        if (model != null) {
            bundle.putParcelable("accessed_calfolder_model", model);
        }
        FragmentTransaction ft = getChildFragmentManager().beginTransaction();
        CalendarDetailFragment fragment = new CalendarDetailFragment();
        fragment.setArguments(bundle);
        ft.replace(R.id.detail_container, fragment);

        if (viewBinding.slidingPaneLayout.isOpen()) {
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        }
        ft.commit();
        viewBinding.slidingPaneLayout.open();
    }

    private void removeDetailContainer() {
        Fragment fragment = getChildFragmentManager().findFragmentById(R.id.detail_container);
        if (fragment != null) {
            getChildFragmentManager().beginTransaction().remove(fragment).commit();
        }
    }
}